home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / SETLEVEL.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  10KB  |  271 lines

  1. /********************************************************************
  2.  *                         SETLEVEL                                 *
  3.  *                                                                  *
  4.  * Written by: Lynn R. Lively                                       *
  5.  * Date:       05/15/90                                             *
  6.  * Version:    1.0                                                  *
  7.  * Written in TurboC V2.00                                          *
  8.  *                                                                  *
  9.  * Allows user input in a MSDOS .BAT file. This program asks a      *
  10.  * specified question and sets the errorlevel according to valid    *
  11.  * answer list. Since MSDOS provides no facility for getting user   *
  12.  * input into a .BAT file, and manipulation of enviorinment strings *
  13.  * seems to be unreliable and nonportable, I have written this      *
  14.  * facility as a possible alternative.                              *
  15.  *                                                                  *
  16.  * I hereby place this program into the public domain.              *
  17.  * Users of this utility do so at their own risk. I accept no       *
  18.  * responsibility for the accuracy or useability of this software.  *
  19.  * However, should you have any questions, suggestions, or problems *
  20.  * I would be happy to talk with you (and help if I can).           *
  21.  * My current work number is 713-591-6111                           *
  22.  *  Your Servant,                                                   *
  23.  *       Lynn R. Lively                                             *
  24.  *                                                                  *
  25.  ********************************************************************/
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <conio.h>
  31.  
  32. /*
  33.  * The following are bit mask values for the option_flg.
  34.  */
  35.  
  36. #define CASE_SENSE       1
  37. #define SINGLE_KEY_ENTRY 2
  38.  
  39. /*
  40.  * The following is the structure deffinition for the valid answer
  41.  * linked list.
  42.  */
  43.  
  44. typedef struct v_ans {
  45.       char *         ans_key;
  46.       int            rtn_err_lev;
  47.       struct v_ans * next_ans;
  48. } VALID_ANS;
  49.  
  50. /*
  51.  * Prototypes of functions defined in this module.
  52.  */
  53.  
  54. void usage (char * progname);
  55.  
  56. int main (int argc, char * argv[])
  57. {
  58.       register int i = 0;
  59.       register int j;
  60.  
  61.       int option_flg = 0;
  62.  
  63.       VALID_ANS * answer_tab = NULL;
  64.       VALID_ANS * wk_ans;
  65.  
  66.       char * wk_ptr;
  67.       char * ques_ptr = "?";
  68.       char   wk_str[256];
  69.  
  70.       if (argc > 1)
  71.       {
  72.         /*
  73.          * Step through and parse the argument list.
  74.          */
  75.  
  76.             for (i = 1; i < argc; i++)
  77.             {
  78.                   j = 0;
  79.                   switch (argv[i][j])
  80.                   {
  81.                   case '/':            /* Options are flagged by '/' */
  82.  
  83.                         j++;
  84.                         switch (argv[i][j])
  85.                         {
  86.                         case 'c':
  87.                         case 'C':
  88.  
  89.                               option_flg |= CASE_SENSE;
  90.                               break;
  91.  
  92.                         case '1':
  93.  
  94.                               option_flg |= SINGLE_KEY_ENTRY;
  95.                               break;
  96.  
  97.                         case 'q':
  98.                         case 'Q':
  99.  
  100.                         /*
  101.                          * The question is assumed to be the argument
  102.                          * after the 'q'.
  103.                          */
  104.  
  105.                               ques_ptr = strdup (argv[++i]);
  106.                               break;
  107.  
  108.                         default:
  109.  
  110.                         /*
  111.                          * Oops! Something wrong. Tell them how its
  112.                          * supposed to work.
  113.                          */
  114.  
  115.                               usage (argv[0]);
  116.                               return (-1);
  117.                         }
  118.                         break;
  119.  
  120.                         case '-':         /* Answers are flagged by '-' */
  121.  
  122.                         /*
  123.                          * The answer is assumed to be everything from the
  124.                          * '-' to the '=' characters. This allows for
  125.                          * single letter and word answers.
  126.                          */
  127.  
  128.                               j++;
  129.                               if ((wk_ptr = strtok (argv[i]+j, "=")) != NULL)
  130.                               {
  131.                               /*
  132.                               * Allocate space for the new answer entry and
  133.                               * link in into the list. Answers are linked
  134.                               * into the list in reverse order from their
  135.                               * command line specification since the logic
  136.                               * is considerably simpler and it shouldn't
  137.                               * matter much anyway.
  138.                               */
  139.  
  140.                                     if (answer_tab == NULL)
  141.                                     {
  142.                                           answer_tab = wk_ans = (VALID_ANS *)
  143.                                                 calloc (1, sizeof(VALID_ANS));
  144.                                     }
  145.                                     else
  146.                                     {
  147.                                           wk_ans = (VALID_ANS *)
  148.                                                 calloc (1, sizeof(VALID_ANS));
  149.                                           wk_ans->next_ans = answer_tab;
  150.                                           answer_tab = wk_ans;
  151.                                     }
  152.  
  153.                               /*
  154.                               * Store the answer string and errorlevel value
  155.                               * into the answer element.
  156.                               */
  157.  
  158.                                     wk_ans->ans_key = strdup (wk_ptr);
  159.                                     wk_ptr          = strtok (NULL, "=");
  160.                                     if (wk_ptr != NULL)
  161.                                     {
  162.                                     /*
  163.                                     * Allow only positive returns since -1 is
  164.                                     * and error return from the program.
  165.                                     */
  166.  
  167.                                           wk_ans->rtn_err_lev =
  168.                                                 abs (atoi (wk_ptr));
  169.                                     }
  170.                                     else  wk_ans->rtn_err_lev = 0;
  171.                               }
  172.                         }
  173.                         break;
  174.  
  175.                   default:
  176.  
  177.                   /*
  178.                    * Oops! Something wrong. Tell them how its
  179.                    * supposed to work.
  180.                    */
  181.  
  182.                         usage (argv[0]);
  183.                         return (-1);
  184.                   }
  185.             }
  186.       }
  187.       else
  188.       {
  189.  
  190.             /*
  191.              * Oops! Something wrong. Tell them how its
  192.              * supposed to work.
  193.              */
  194.  
  195.             usage (argv[0]);
  196.             return (-1);
  197.       }
  198.  
  199.       /*
  200.        * Ask the question and get their answer.
  201.        */
  202.  
  203.       printf ("%s", ques_ptr);
  204.  
  205.       if ((option_flg & SINGLE_KEY_ENTRY) != 0)
  206.       {
  207.             wk_str[0] = getche ();
  208.             wk_str[1] = '\0';
  209.       }
  210.       else  fgets (wk_str, sizeof (wk_str), stdin);
  211.  
  212.       /*
  213.        * See if we can find their answer in our valid answer list.
  214.        */
  215.  
  216.       wk_ans = answer_tab;
  217.       while (wk_ans != (VALID_ANS *) NULL)
  218.       {
  219.             if ((option_flg & CASE_SENSE) != 0)
  220.             {
  221.                   if ((strncmp (wk_str, wk_ans->ans_key,
  222.                         (strlen (wk_ans->ans_key)))) == 0)
  223.                   {
  224.                   /*
  225.                    * If we found the answer return the associated
  226.                    * errorlevel return number.
  227.                    */
  228.  
  229.                         return (wk_ans->rtn_err_lev);
  230.                   }
  231.             }
  232.             else
  233.             {
  234.                   if ((strnicmp (wk_str, wk_ans->ans_key,
  235.                         (strlen (wk_ans->ans_key)))) == 0)
  236.                   {
  237.                   /*
  238.                    * If we found the answer return the associated
  239.                    * errorlevel return number.
  240.                    */
  241.  
  242.                         return (wk_ans->rtn_err_lev);
  243.                   }
  244.             }
  245.  
  246.             wk_ans = wk_ans->next_ans;
  247.       }
  248.  
  249.       /*
  250.        * If the answer wasn't in our table return a -1.
  251.        */
  252.  
  253.       return (-1);
  254. }
  255.  
  256. void usage (char * progname)
  257. {
  258.       printf ("%s usage:\n", progname);
  259.       printf ("%s /[options] -<valid answer>=<errorlevel>\n\n", progname);
  260.       printf ("Where:\n");
  261.       printf ("  options      = c    (Answers are case sensitive)\n");
  262.       printf ("                 1    (Single key entry)\n");
  263.       printf ("                 q\"Question Text\"\n\n");
  264.       printf ("  valid answer = (Single letter or word response)\n");
  265.       printf ("  errorlevel   = (errorlevel to return when answer seen)\n\n");
  266.       printf ("Example:\n%s /q\"Answer Y or N: \" /1 -y=1 -n=2\n\n",
  267.             progname);
  268.       printf ("Note: -1 is returned if answer is not valid or syntax\n");
  269.       printf ("      is incorrect.\n");
  270. }
  271.